﻿namespace SharePointCSVImporter.RuleEditors
{
    using System.Windows.Controls;

    public partial class StringReplaceRuleEditor : UserControl
    {
        #region Fields

        private CSVParser.FieldMapping _mapping;

        #endregion Fields

        #region Constructors

        public StringReplaceRuleEditor(ref CSVParser.FieldMapping mapping)
        {
            InitializeComponent();
            _mapping = mapping;
            if (mapping != null && mapping.Transformation != null && !string.IsNullOrEmpty(mapping.Transformation.TransformRule))
            {
                if (this._mapping.Transformation.TransformRule.StartsWith("Left:") || this._mapping.Transformation.TransformRule.StartsWith("Right:"))
                {
                    string[] parts = mapping.Transformation.TransformRule.Split(':');
                    if (parts.Length > 0)
                    {
                        this.NumOfChars.Text = parts[1];
                        string direction = parts[0].Trim();
                        if(direction.Equals("Right"))
                        {
                            this.TrimDiretionComboBox.SelectedIndex = 1;
                        }

                    }

                }
                else
                {
                    this.LookupText.Text = _mapping.Transformation.TransformRule;
                }
                this.ReplaceText.Text = _mapping.Transformation.TransformValue;
            }
        }

        #endregion Constructors

        #region Methods

        private void LookupText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(LookupText.Text))
            {
                _mapping.Transformation.TransformRule = LookupText.Text;
            }
            if (string.IsNullOrEmpty(_mapping.Transformation.TransformValue))
            {
                _mapping.Transformation.TransformValue = "";
            }
        }

        private void NumOfChars_TextChanged(object sender, TextChangedEventArgs e)
        {
            ReadNumOfCharsToTrim();
        }

        private void ReadNumOfCharsToTrim()
        {
            if (TrimDiretionComboBox !=null && TrimDiretionComboBox.SelectedIndex > -1)
            {
                if (!string.IsNullOrWhiteSpace(NumOfChars.Text))
                {
                    int num = 0;
                    if (int.TryParse(NumOfChars.Text, out num))
                    {

                        var selectedItem = (ComboBoxItem)TrimDiretionComboBox.SelectedItem;
                        string trimDirection = selectedItem.Content.ToString();
                        _mapping.Transformation.TransformRule = trimDirection + ":" + num.ToString();
                        if (string.IsNullOrEmpty(_mapping.Transformation.TransformValue))
                        {
                            _mapping.Transformation.TransformValue = "";
                        }
                    }
                }
            }
        }

        private void ReplaceText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ReplaceText.Text))
            {
                _mapping.Transformation.TransformValue = ReplaceText.Text;
            }
        }

        private void TrimDiretionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ReadNumOfCharsToTrim();
        }

        #endregion Methods
    }
}